<?php
    defined('ACC')||exit('ACC Denied'); 
    class accountingmodel extends model{
        
        public function getaccountlist($company_id, $category = ''){
            if($category == ''){
                $sql = 'select * from account_list where account_level = 1';            
            }else{
                $sql = "select * from account_list where account_category = '" . $category . "' and account_level = 1";
            }
            return $this->db->getAll($sql);        
        }
        
        public function getsettledbooking($company_id, $account_id){
                
                 $sql = "select * from account_daily_booking where account_id =" . $account_id . " and posted = 2 and deleteflg not in ('deleted', 'archived' )";
                 $allsettled = $this->db->getAll($sql);
                 $settled = array();
                 foreach($allsettled as $k=>$v){
                     $actionlogid = $v['action_log_id'];
                     $sql = 'select company_id from action_log where action_log_id = ' . $actionlogid;
                     $companyid = $this->db->getOne($sql);
                     if($companyid == $company_id){
                         $settled[] = $v;                     
                     }
                 }
                 return $settled;
        }
        
        public function getaccountbalance($company_id, $account_id = ''){
                if($account_id == ''){
                    $sql = "select * from account_balance where company_id = " . $company_id . " and deleteflg not in ('deleted', 'archived' )"; 
                    return $this->db->getAll($sql);
                }else{
                    $sql = "select * from account_balance where account_id =" . $account_id . " and company_id = " . $company_id . " and deleteflg not in ('deleted', 'archived' )";                                                                                                                                                             
                    return $this->db->getRow($sql);
                }
        }
            
        public function getpostedbooking($company_id, $account_id){
                
                 $sql = "select * from account_daily_booking where account_id =" . $account_id . " and posted = 1 and deleteflg not in ('deleted', 'archived' )";
                 $allsettled = $this->db->getAll($sql);
                 $settled = array();
                 foreach($allsettled as $k=>$v){
                     $actionlogid = $v['action_log_id'];
                     $sql = 'select company_id from action_log where action_log_id = ' . $actionlogid;
                     $companyid = $this->db->getOne($sql);
                     if($companyid == $company_id){
                         $settled[] = $v;                     
                     }
                 }                                                            
                 return $settled;
        }
        
    public function getchildaccountbalance($company_balance_tagid){        
        }
        
        
    public function getaccountinfo($account_id){
       $sql= 'select * from account_list where account_id = ' . $account_id;
       return $this->db->getRow($sql); 
   }
   
   public function  getdailybooking($action_log_id){
       $sql = "select * from account_daily_booking where action_log_id = " .$action_log_id. " and deleteflg not in ('deleted', 'archived' )";
       $row = $this->db->getAll($sql);
       return $row;
   }     
        
    public function post($input_id){
       $arr = array('posted' => 1);
       return $this->db->autoExecute($arr, 'account_daily_booking', 'update', ' where input_id = ' . $input_id);
   }
   
   public function revpost($input_id){
       $arr = array('posted' => 0);
       return $this->db->autoExecute($arr, 'account_daily_booking', 'update', ' where input_id = ' . $input_id);
   }
   
   public function  getposteddailybooking($account_id){
       $sql = "select * from account_daily_booking where account_id = " . $account_id . " and posted = 1 and deleteflg not in ('deleted', 'archived' )";    
       $row = $this->db->getAll($sql);
       return $row;       
   }
   
   public function settleaccount($company_id, $account_id){       
       $postedbooking = $this->getpostedbooking($company_id, $account_id);
       $change = 0;
       foreach($postedbooking as $k=>$v){
           $amount = $v['amount'];
           $debitcredit = $v['debit_credit'];
           if ($debitcredit == 'debit'){
           $change = $change + $amount;           
           }else{
           $change = $change - $amount;
           }
           
           $inputid = $v['input_id'];
           $arr = array('posted' =>2);
            $this ->db->autoExecute($arr, "account_daily_booking", "update", " where account_id =" . $account_id . " and input_id = " . $inputid . " and deleteflg not in ('deleted', 'archived' )");
       }
       
       
       $account = $this->getaccountbalance($company_id, $account_id);
       if(empty($account)){
           if($change>=0){
               $data = array('account_id'=>$account_id, 'company_id'=>$company_id, 'account_balance'=>$change, 'debit_credit'=>'debit'); 
               $this->table('account_balance');
               $this->add($data);
           }else{
               $data = array('account_id'=>$account_id, 'company_id'=>$company_id, 'account_balance'=>-$change, 'debit_credit'=>'credit'); 
               $this->db->table('account_balance');
               $this->db->add($data); 
           } 
       }else{
           $balance = $account['account_balance'];
           $debitcredit = $account['debit_credit'];
           if($debitcredit == 'debit'){
            $end = $balance;
            }else{
            $end = - $balance;
            }
            $end = $end + $change;
            if($end>=0){
               $arr = array('account_balance' => $end, 'debit_credit'=>'debit');
               $this ->db->autoExecute($arr, "account_balance", "update", " where account_id =" . $account_id . " and company_id = " . $company_id . " and deleteflg not in ('deleted', 'archived' )");  
            }else{
               $arr = array('account_balance' => -$end, 'debit_credit'=>'credit');
               $this ->db->autoExecute($arr, "account_balance", "update", " where account_id =" . $account_id . " and company_id = " . $company_id . " and deleteflg not in ('deleted', 'archived' )");  
            }
       }
   }
   
   public function generatefinancialreports($companyid, $companyy, $companyq){
       $this->db->autoExecute(array('amount'=>0), "financial_report", "update", "  where company_id = "  . $companyid . " and company_y = " . $companyy . " and company_q = " . $companyq .  " and deleteflg not in ('deleted', 'archived' )" );
       $company = new companymodel();
       $actionlog = $company->getactionlogbyyq($companyid, $companyy, $companyq);
       //$dailybooking = array();

       //此处判断是否还有为过账的凭证，或者未结账的账户       
       foreach($actionlog as $k=>$v){
            $logid = $v['action_log_id'];
            $booking = $this ->getdailybooking($logid);
            foreach($booking as $key=>$value){
                if($value['posted'] !=2){
                    return false;
                }
            }            
        }
        //以下代码判断是否已经完成本年度或者季度的经营
        // 这段代码还没有写，主要是根据这个季度是否已经完成结转、计提税收等来判断
        //以上代码判断是否已经完成本年度或者季度的经营
        $accountbalance = $this->getaccountbalance($companyid);
        
        //以下代码遍历每一个账户，并写入财务报表。但是本端代码没有考虑存在二级、三级账户的情形。如果存在的二三级账户的话，将会重复读取数据，因此未来需要改写代码
        foreach($accountbalance  as $k=>$v){
            $accountid = $v['account_id'];
            $accountinfo = $this->getaccountinfo($accountid);
            
            if($accountinfo['account_category'] != '损益'){
                $debitcredit =  $v['debit_credit'];
                if(($accountinfo['account_category'] == '资产')||($accountinfo['account_category'] == '成本')){
                    if($debitcredit == 'debit'){
                        $sign = 1;
                    }else{
                        $sign = -1;
                    }                       
                }else{
                    if($debitcredit == 'debit'){
                        $sign = -1;
                    }else{
                        $sign = 1;
                    } 
                }
                $amount = $sign * $v['account_balance'];
                  
                $itemid = $accountinfo['item_id'];
                $sql = "select * from financial_report where company_id = "  . $companyid . " and item_id = " . $itemid . " and company_y = " . $companyy . " and company_q = " . $companyq .  " and deleteflg not in ('deleted', 'archived' )";
                $exist = $this->db->getRow($sql);
                if(empty($exist)){
                   $arr = array('company_id'=>$companyid, 'item_id'=>$itemid, 'amount'=>$amount, 'company_y'=>$companyy, 'company_q'=>$companyq);
                   $this->db->autoExecute($arr, 'financial_report');                  
                }else{
                    $amount = $amount + $exist['amount'];
                    $tagid = $exist['financial_report_tagid'];
                    $arr = array('amount'=>$amount);
                    $this->db->autoExecute($arr, "financial_report", "update"," where financial_report_tagid = " . $tagid);
                }
            }else{
                //生成利润表 
            }
        }
        return true;
   }
   
   
   public function getfinancialreport($company_id, $company_y, $company_q, $table, $item_category = ''){
       $array = array(); 
       if ($item_category == ''){
            $sql = 'select * from financial_report_item_list where item_table = "' . $table . '"';
        }else{
            $sql = 'select * from financial_report_item_list where item_table = "' . $table . '" and item_category = "' . $item_category . '"';
        }
        $itemlist = $this->db->getAll($sql);
        foreach($itemlist  as $k=>$v){
            $itemid = $v['item_id'];
            $itemname = $v['item_name_ch'];
            $sql = "select * from financial_report where company_id = "  . $company_id . " and company_y = " . $company_y . " and company_q = " . $company_q .  " and item_id = " . $itemid .  " and deleteflg not in ('deleted', 'archived' )";
            $itemrow = $this->db->getRow($sql);
            if(empty($itemrow)){
                $array[$itemname] = 0;            
            }else{
                $array[$itemname] = $itemrow['amount']; 
            }
        }
        
        return $array;       
   }
   
   
   public function carryover($company_id, $action_log_id){
         $sql = 'select * from company where company_id = ' . $company_id;
         $companyinfo = $this->db->getRow($sql);
         $company_y = $companyinfo['company_y'];
         $company_q = $companyinfo['company_q'];
         
         $sql = 'select * from account_list where account_category = "损益" and account_level = 1' ;
         $pnlaccount = $this->db->getAll($sql);
         
         
         foreach($pnlaccount as $k=>$v){
             $accountid = $v['account_id'];
             $sql = "select * from account_daily_booking where account_id = " . $accountid . " and deleteflg not in ('deleted', 'archived' )";
             $dailybooking = $this->db->getAll($sql);
             $balance = 0;
             foreach($dailybooking as $key=>$value){
                 $actionlogid = $value['action_log_id'];
                 $sql = 'select company_id from action_log where action_log_id = ' . $actionlogid;
                 $companyid = $this->db->getOne($sql);
                 $sql = 'select action_log_y from action_log where action_log_id = ' . $actionlogid; 
                 $actionlogy = $this->db->getOne($sql);
                 $sql = 'select action_log_q from action_log where action_log_id = ' . $actionlogid; 
                 $actionlogq = $this->db->getOne($sql);                 
                 
                 if(($companyid == $company_id)&&($actionlogy==$company_y)&&($actionlogq==$company_q)) {
                     $amount = $value['amount'];
                     $debitcredit = $value['debit_credit'];
                     if($debitcredit == 'debit'){
                            $balance = $balance + $amount;                         
                     }else{
                            $balance = $balance - $amount;   
                     }
                 }                 
             }
             
             if($balance>0){
                 $arr = array('action_log_id'=>$action_log_id, 'account_id'=>4103, 'amount'=>$balance, 'debit_credit'=>'debit', 'practised'=>0, 'posted'=>0);
                 $this->db->autoExecute($arr, 'account_daily_booking', 'insert');  
                 $arr = array('action_log_id'=>$action_log_id, 'account_id'=>$accountid, 'amount'=>$balance, 'debit_credit'=>'credit', 'practised'=>0, 'posted'=>0);
                 $this->db->autoExecute($arr, 'account_daily_booking', 'insert');
             }elseif($balance<0){
                 $arr = array('action_log_id'=>$action_log_id, 'account_id'=>$accountid, 'amount'=>-$balance, 'debit_credit'=>'debit', 'practised'=>0, 'posted'=>0);
                 $this->db->autoExecute($arr, 'account_daily_booking', 'insert');
                 $arr = array('action_log_id'=>$action_log_id, 'account_id'=>4103, 'amount'=>-$balance, 'debit_credit'=>'credit', 'practised'=>0, 'posted'=>0);
                 $this->db->autoExecute($arr, 'account_daily_booking', 'insert');  
             }
         }
         return true;         
   }

   
   
   
      
}  
?>
